#include "drv_seg.h"
#include "drv_sht20.h"
#include "drv_spi.h"
#include "drv_systick.h"
static uint8_t d[]={0x3F, 0x06,0x5B,  0x4F, 0x66,  0x6D, 0x7D, 0x07, 0x7F,  0x6F,0x00};
static uint8_t w[]={0x08, 0x04, 0x02,0x01};
                       
void drv_74hc595_init(void)
{
	drv_spi2_init();
}
void drv_seg_display(uint8_t wei,uint8_t duan)
{
	drv_spi2_senddata(w[wei]);
    if(wei == 2)
    {
        drv_spi2_senddata(d[duan]+0x80);
    }
    else
    {
        drv_spi2_senddata(d[duan]);
    }
	GPIO_ResetBits(GPIOB, GPIO_Pin_12);
	drv_systick_ms(1);
	GPIO_SetBits(GPIOB, GPIO_Pin_12);

}
void drv_seg_temp(void)
{
	int one=0,two=0,three=0,four=0;
	int value=0;
    int i=400;
    drv_seg_display(2, 0);
    drv_systick_ms(100);
	value=(int)(drv_sht20_gettemp()*100);
    one=(value/1000)%10;
    two=(value/100)%10;
    three=(value/10)%10;
    four=value%10;

    while(i)
    {
        drv_seg_display(0, four);
        drv_systick_ms(1);
        drv_seg_display(1, three);
        drv_systick_ms(1);
        drv_seg_display(2, two);
        drv_systick_ms(1);
        drv_seg_display(3, one);
        drv_systick_ms(1);
        i--;
    }
    drv_seg_display(2, 0);
}
void drv_seg_hum(void)
{
	int one=0,two=0,three=0,four=0;
	int value=0;
    int i=400;
    drv_seg_display(2, 0);
    drv_systick_ms(100);
	value=(int)(drv_sht20_gethum()*100);
    one=(value/1000)%10;
    two=(value/100)%10;
    three=(value/10)%10;
    four=value%10;

	while(i)
    {
        drv_seg_display(0, four);
        drv_systick_ms(1);
        drv_seg_display(1, three);
        drv_systick_ms(1);
        drv_seg_display(2, two);
        drv_systick_ms(1);
        drv_seg_display(3, one);
        drv_systick_ms(1);
        i--;
    }
    drv_seg_display(2, 0);
}
